#include "test.h"
 using namespace std;
StudentManager::StudentManager() {
    conn = mysql_init(NULL);
    // �����ַ�����
    // mysql_options(conn, MYSQL_SET_CHARSET_NAME, "GBK");
    if(!mysql_real_connect(conn,host,user,pwd,dbName,port,NULL,0)) {
        std::cout<<"Failed to connect"<<std::endl;
        exit(1);
    }
      
    
}
 
StudentManager::~StudentManager() {
    mysql_close(conn);
}
bool StudentManager::insertStu(Student &stu) {
    char sql[1024];

    sprintf(sql,"insert into student (stuId,stuName,className,password) values(%d,'%s','%s','%s')",
            stu.stuId,stu.stuName.c_str(),stu.className.c_str(),stu.password.c_str());

    if(mysql_query(conn,sql)) {
        fprintf(stderr,"Failed to insert data into database!!!Error:%s\n",
                mysql_error(conn));
        return false;
    }
    return true;
}
 
// c_str()������һ��const char*ָ�룬ָ���Կ��ַ���ֹ������
bool StudentManager::updateStu(Student &stu) {
    char sql[1024];
    sprintf(sql,"UPDATE student SET stuName = '%s',className = '%s'"
    "where stuId = %d",stu.stuName.c_str(),stu.className.c_str(),stu.stuId);
    if(mysql_query(conn,sql)) {
        fprintf(stderr,"Failed to update data!!!Error:%s\n",mysql_error(conn));
        return false;
    }
    return true;
}
 
bool StudentManager::deleteStu(int stuId) {
    char sql[1024];
    sprintf(sql,"DELETE FROM student WHERE stuId = '%d'",stuId);
    if(mysql_query(conn,sql)) {
        fprintf(stderr,"Failed to delete data!!!Error:%s\n",mysql_error(conn));
        return false;
    }
    return true;
}
 
vector<Student> StudentManager::queryStu(string condition) {
    vector<Student> stuList;
    char sql[1024];
    sprintf(sql,"SELECT * FROM student %s",condition.c_str());
    if(mysql_query(conn,sql)) {
        fprintf(stderr,"Failed to select data!!!Error:%s\n",mysql_error(conn));
        return {};
    }
    MYSQL_RES* res = mysql_store_result(conn);
    MYSQL_ROW row;
    while((row = mysql_fetch_row(res))) {
        Student stu;
        stu.stuId = atoi(row[0]);
        stu.stuName = row[1];
        stu.className = row[2];
        stu.password = row[3];
        stuList.push_back(stu);
    }
    return stuList;
}
int  showmain()
{
    	printf("欢迎来到风雨无阻聊天室输入1 登录 输入 2注册 输入3 退出");
	int num;
	cin >> num;
	
	switch (num) {
		case 1: 
		{
			string name ;
			string password;
			cout << "请输入你的账号 :  " << endl;
			cin >> name;
			cout << "请输入你的密码 :  " << endl;
			cin >> password;
			char condition[1024];
			 sprintf(condition," WHERE stuName = '%s' AND password = '%s';",name.c_str(),password.c_str());
			   vector<Student> ret= StudentManager::GetInstance()->queryStu(condition);
               int data;
      for(auto& it:ret){
        std::cout<<" ";
        cout<<it.stuId<<" "<<it.stuName<<" "<<it.className  <<endl;
        data = it.stuId;
        }
       if(!ret.empty())
       {
        return data;
       }
			 break;
           
		}
		case 2 : {
			string name ;
			string className;
			string password;
			int count ;
			cout << "序号" << endl;
			cin>> count;
	        cout << "姓名:  " << endl;
			cin >> name;
			cout << "小组" << endl;
			cin >> className;
			cout << "密码 :  " << endl;
			cin >> password;
			 Student stu{count,name,className,password};
    StudentManager::GetInstance()->insertStu(stu);
		}
		default: exit(0);
	}
	return  0;
}